<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="keywordFormRules" :model="keywordForm" label-position="left" label-width="140px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="keywordForm.title" />
      </el-form-item>
      <el-form-item label="类型" prop="emotion">
         <el-radio-group v-model="keywordForm.emotion">
          <el-radio :label="1">通用</el-radio>
          <el-radio :label="2">正面</el-radio>
          <el-radio :label="3">负面</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="keywordForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="keywordForm.status.value === 5" type="danger" @click="handleDelete(keywordForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveKeyword, removeKeywordByID, getKeywordDetail } from '@/api/knowledge/keyword'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      keywordForm: {
        code: '',
        title: '',
        remark: '',
        emotion:1,
        status: {
          label: '',
          value: 6
        }
      },
      keywordFormRules: []
    }
  },
  created() {
    let keywordID = this.$route.query && this.$route.query.keywordID
    if (keywordID !== undefined) {
      keywordID = parseInt(keywordID)
      if (!isNaN(keywordID)) {
        this.listLoading = true
        const formData = {
          'keywordID': keywordID
        }
        getKeywordDetail(formData).then(response => {
          if (response.status === 200) {
            this.keywordForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(keywordID)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'keyword' })
    },
    createData() {
      this.listLoading = true
      const keywordForm = this.keywordForm
      keywordForm.status = keywordForm.status.value
      console.log(keywordForm)
      if (keywordForm.ID > 0) {
        keywordForm.keywordID = keywordForm.ID
      }
      saveKeyword(keywordForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'keyword' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const fromdata = {
        'keywordID': row.ID
      }
      removeKeywordByID(fromdata).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'keyword' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
